<?php

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/5/24
 * Time: 13:21
 */
class PrintMsgtpl extends BaseMsgtpl
{
    /**
     * 发指派订单消息
     */
    public function sendAssignMessage($suborderId,$accountId,$confId){
        $orderInfo = M('PrintSuborderRecord')->where(array('suborder_id'=>$suborderId))->find();
        $wechat = M('PrintReceiverInfo')->where(array('receiver_id'=>$orderInfo['receiver_recvid']))->getField('wechat_user_id');
        if(!$wechat){
            $wechat = M('PrintReceiverInfo')->where(array('receiver_id'=>$orderInfo['delivery_recvid']))->getField('wechat_user_id');

        }
        $condition = array(
            'wechat_user_id' => $wechat,
            'account_id' => $accountId
        );

        //查询微信用户信息
        $wechatUser = D('Wechat/WechatUser')->getInfo($condition,'openid,subscribe');

        $url = C('site_url').'/base/PrintTaking/orderCenter/conf_id/'.$confId;
        if($wechatUser['subscribe']) {
//            $this->wxTplSign = '_hHj0eU1ZQ7yorFomzpQvFV21NxQj8S_bqolOO9sbBk';
            $this->wxTplSign = 'dq_HYN94TDxj8I95hAblmtBFPsNbmPZVa1ePnVfOy4M';
            //云感物联的一个模板 keyword一样测试用
            //_hHj0eU1ZQ7yorFomzpQvFV21NxQj8S_bqolOO9sbBk

            //组消息内容
            $info=array(
                'first' =>array('value'=>'您有一条新订单！'),
                'keyword1' =>array('value'=>$this->gettype($orderInfo['order_type'])),
                'keyword2' =>array('value'=>date('Y年m月d日 H:i:s',$orderInfo['create_time'])),
                'keyword3' =>array('value'=>$orderInfo['content']),
                'remark' =>array('value'=>'请及时处理！'),
            );

            $this->wxObj->messageTpl($this->wechatInfo['appid'], $this->wechatInfo['appsecret'], $wechatUser['openid'], $this->wxTplSign, $info, $url);
        }
        return true;
    }

    /**
     * 发接单消息
     */
    public function sendReceiveMessage($orderId,$accountId,$confId){
        $orderInfo = M('PrintOrder')->where(array('order_id'=>$orderId))->find();
        $wechat = M('PrintClient')->where(array('client_id'=>$orderInfo['client_id']))->getField('user_id');

        $condition = array(
            'user_id' => $wechat,
            'account_id' => $accountId
        );

        //查询微信用户信息
        $wechatUser = D('Wechat/WechatUser')->getInfo($condition,'openid,subscribe');

        $url = C('site_url').'/base/PrintClient/orderCenter/conf_id/'.$confId.'/type/'.$orderInfo['order_type'];
        if($wechatUser['subscribe']) {
//            $this->wxTplSign = '_hHj0eU1ZQ7yorFomzpQvFV21NxQj8S_bqolOO9sbBk';
            $this->wxTplSign = 'dq_HYN94TDxj8I95hAblmtBFPsNbmPZVa1ePnVfOy4M';
            //云感物联的一个模板 keyword一样测试用
            //_hHj0eU1ZQ7yorFomzpQvFV21NxQj8S_bqolOO9sbBk

            //组消息内容
            $info=array(
                'first' =>array('value'=>'您的订单已接单！'),
                'keyword1' =>array('value'=>$this->gettype($orderInfo['order_type'])),
                'keyword2' =>array('value'=>date('Y年m月d日 H:i:s',$orderInfo['create_time'])),
                'keyword3' =>array('value'=>$orderInfo['order_content']),
                'remark' =>array('value'=>'请及时处理！'),
            );

            $r = $this->wxObj->messageTpl($this->wechatInfo['appid'], $this->wechatInfo['appsecret'], $wechatUser['openid'], $this->wxTplSign, $info, $url);
            print_r($r);die;
        }
        return true;
    }

    /*
     * 配送完成 给用户发送消息
     */
    public function sendSenderMessage($orderId,$accountId,$confId){
        $orderInfo = M('PrintOrder')->where(array('order_id'=>$orderId))->find();
        $userId = M('PrintClient')->where(array('client_id'=>$orderInfo['client_id']))->getField('user_id');
        $condition = array(
            'user_id' => $userId,
            'account_id' => $accountId
        );
        //查询微信用户信息
        $wechatUser = D('Wechat/WechatUser')->getInfo($condition,'openid,subscribe');
        $url = C('site_url').'/base/PrintClient/orderCenter/conf_id/'.$confId.'/type/'.$orderInfo['order_type'];
        if($wechatUser['subscribe']){
//            $this->wxTplSign = '_hHj0eU1ZQ7yorFomzpQvFV21NxQj8S_bqolOO9sbBk';
            $this->wxTplSign = 'dq_HYN94TDxj8I95hAblmtBFPsNbmPZVa1ePnVfOy4M';
            //组消息内容
            $info=array(
                'first' =>array('value'=>'您的订单已配送完成！'),
                'keyword1' =>array('value'=>$this->gettype($orderInfo['order_type'])),
                'keyword2' =>array('value'=>date('Y年m月d日 H:i:s',$orderInfo['create_time'])),
                'keyword3' =>array('value'=>$orderInfo['order_content']),
                'remark' =>array('value'=>''),
            );

            $this->wxObj->messageTpl($this->wechatInfo['appid'], $this->wechatInfo['appsecret'], $wechatUser['openid'], $this->wxTplSign, $info, $url);
        }
    }
    /**
     * 给所有人发有新订单消息
     */
    public function sendAllMessage($suborderId,$accountId,$confId,$wechatId){
        $orderInfo = M('PrintSuborderRecord')->where(array('suborder_id'=>$suborderId))->find();
        $condition = array(
            'wechat_user_id' => $wechatId,
            'account_id' => $accountId
        );
        $orderContent = M('PrintOrder')->where(array('order_id'=>$orderInfo['order_id']))->getField('order_content');

        //查询微信用户信息
        $wechatUser = D('Wechat/WechatUser')->getInfo($condition,'openid,subscribe');
        $url = C('site_url').'/base/PrintTaking/orderCenter/conf_id/'.$confId;
        if($wechatUser['subscribe']) {
            $this->wxTplSign = 'dq_HYN94TDxj8I95hAblmtBFPsNbmPZVa1ePnVfOy4M';
//            $this->wxTplSign = 'dq_HYN94TDxj8I95hAblmtBFPsNbmPZVa1ePnVfOy4M';
//            $this->wxTplSign = '_hHj0eU1ZQ7yorFomzpQvFV21NxQj8S_bqolOO9sbBk';
            //云感物联的一个模板 keyword一样测试用

            //组消息内容
            $info=array(
                'first' =>array('value'=>'您有一条新订单！'),
                'keyword1' =>array('value'=>$this->gettype($orderInfo['order_type'])),
                'keyword2' =>array('value'=>date('Y年m月d日 H:i:s',$orderInfo['create_time'])),
                'keyword3' =>array('value'=>$orderContent),
                'remark' =>array('value'=>'请及时处理！'),
            );
            $this->wxObj->messageTpl($this->wechatInfo['appid'], $this->wechatInfo['appsecret'], $wechatUser['openid'], $this->wxTplSign, $info, $url);
        }
        return true;
    }

    /**
     * 发取消订单消息
     */
    public function sendCancleMessage($suborderId,$accountId,$confId)
    {
        $orderInfo = M('PrintSuborderRecord')->where(array('suborder_id'=>$suborderId))->find();
        $order = M('PrintOrder')->where(array('order_id'=>$orderInfo['order_id']))->find();
        $wechat = M('PrintClient')->where(array('client_id'=>$order['client_id']))->getField('user_id');
        $wechatId = M('WechatUser')->where(array('user_id'=>$wechat))->getField('wechat_user_id');
        $condition = array(
            'wechat_user_id' => $wechatId,
            'account_id' => $accountId
        );
        //查询微信用户信息
        $wechatUser = D('Wechat/WechatUser')->getInfo($condition,'openid,subscribe');
        $url = C('site_url').'/base/PrintClient/orderCenter/conf_id/'.$confId.'/type/'.$orderInfo['order_type'];
        if($wechatUser['subscribe']) {
//            $this->wxTplSign = '_hHj0eU1ZQ7yorFomzpQvFV21NxQj8S_bqolOO9sbBk';
            $this->wxTplSign = 'dq_HYN94TDxj8I95hAblmtBFPsNbmPZVa1ePnVfOy4M';
            //云感物联的一个模板 keyword一样测试用

            //组消息内容
            $info=array(
                'first' =>array('value'=>'您的订单已取消！'),
                'keyword1' =>array('value'=>$this->gettype($orderInfo['order_type'])),
                'keyword2' =>array('value'=>date('Y年m月d日 H:i:s',$orderInfo['create_time'])),
                'keyword3' =>array('value'=>$order['order_content']),
                'remark' =>array('value'=>''),
            );
            $this->wxObj->messageTpl($this->wechatInfo['appid'], $this->wechatInfo['appsecret'], $wechatUser['openid'], $this->wxTplSign, $info, $url);
        }
        return true;
    }

    public function gettype($type){
        if($type == 1){
            return '图文制作';
        }
        elseif($type == 2){
            return '电脑维修';
        }
        elseif($type == 3){
            return '耗材配送';
        }else{
            return '未知类型';
        }
    }
}